<?php

/**
* 会员管理
*
* @package		Hooloo framework
* @author 		Bill
* @copyright 	Hooloo Co.,Ltd
* @version		1.0
* @release		2017.05.08
*/
defined('BASEPATH') OR exit('No direct script access allowed');

class User extends Controller{
	public function __construct(){
		parent::__construct();
	}
	
	/**
	* 列表
	*/
	public function index($aid = 0, $demo_account = 0, $username = 0, $p = 1){
		//接收参数
		$aid = intval($aid);
		$demo_account = intval($demo_account);
		$username = urldecode(sql_format($username));
		$p = intval($p);
		
		//指定代理商
		if($aid == 0){
			$where = " where aid > 0";
		} else{
			$where = " where aid = $aid";
		}
		
		//帐号类型
		if($demo_account == 0){
			$where .= " and demo_account = 0";
		} elseif($demo_account == 1){
			$where .= " and demo_account = 1";
		} else{
			$demo_account = 2;
		}
		
		//存在搜索
		if($username){
			if (is_numeric($username)) {
				$where .= " and id = " . $username;
			} else {
				$where .= " and (username like '%$username%' or realname like '%$username%')";
			}
		} else{
			$username = 0;
		}
		
		//数据分页
		$pize = 15;//每页数量
		if(0 >= $p){
			$p = 1;
		}
		$start = ($p - 1) * $pize;
		
		//查询总数
		$sql = "select count(*) as c from user" . $where;
		$total = $this->db->query($sql)->row_array();
		$total = $total["c"];
		
		//分页查询
		$sql = "select * from user " . $where . " order by id desc limit $start, $pize";
		$list = $this->db->query($sql)->result_array();
		
		//分页展示
		$url = "/" . $this->_controller . '/' . $this->_action . '/' . $aid . "/". $demo_account . "/" . $username . "/";
		$page = page_format($total, $pize, $p, $url);
		
		//查询所有代理商
		$sql = "select * from agent";
		$agent = $this->db->query($sql)->result_array();
		
		//分配展示
		$this->assign("list", $list);
		$this->assign("agent",$agent);
		$this->assign("page", $page);
		$this->assign("total", $total);
		
		$this->assign("aid", $aid);
		$this->assign("demo_account", $demo_account);
		$this->assign("username", $username);
		$this->display();
	}
	
	/**
	* 新增
	* @param	string	$username //帐号
	* @param	string	$password //密码
	* @param	string	$sex //性别
	* @param	string	$nickname //昵称
	* @param	string	$email //邮箱
	* @param	string	$qq //qq
	* @param	int		$aid //代理商id
	* @param	string	$agent_name //代理商名称
	* @param	int		$sid //员工id
	* @param	string	$staff_name //员工姓名
	* @param	string	$demo_account //帐号类型,是否试玩帐号，0，否，1，是		
	//交易规则
	* @param	int		$stock_type //融资融券类型，1：融资，2：融券，0：无限制
	* @param	int		$max_amount //最大持仓总额(资金)
	* @param	float	$buy_fee //买入手续费
	* @param	float	$sell_fee //卖出手续费
	* @param	float	$save_cost //结算费用
	* @param	float	$stamp //印花税
	* @param	int		$min_num //最小股数
	* @param	int		$max_num //最大股数
	* @param	int		$min_time //最小卖出时间
	* @param	int		$rmin_time //融券最小卖出时间
	* @param	int		$single_limit //单笔额度限额
	* @param	int		$upper_limit //单股持仓总额上限
	* @param	int		$money_max //最高可用额度限制
	* @param	int		$multiple //资金倍数
	* @param	int		$is_only_rstock //是否只能做带R股票
	* @param	int		$is_early_jiancang //是否提前禁止建仓
	//银行卡信息
	* @param	string	$realname //真实姓名
	* @param	string	$sex //性别
	* @param	string	$bank_account //银行开户账户名称
	* @param	string	$bank_name //收款银行
	* @param	string	$bank_code //银行账号
	* @param	string	$bank_address//开户行(支行名称)，如郑州交行东风路支行
	* @param	string	$mobile //手机号
	* @param	string	$atmpwd //取款密码
	* @param	array	$result 	返回结果
	*/
	public function add(){
		if(IS_POST){
			//基础信息
			$username = isset($_POST["username"]) ? substr(sql_format($_POST["username"]), 0, 20) : "";//帐号
			$password = empty($_POST["password"]) ? "" : md5($_POST["password"]) ;//密码
			$nickname = isset($_POST["nickname"]) ? mb_substr(sql_format($_POST["nickname"]), 0, 10) : "";//昵称
			$email = isset($_POST["email"]) ? mb_substr(sql_format($_POST["email"]), 0, 50) : "";//邮箱
			$qq = isset($_POST["qq"]) ? mb_substr(sql_format($_POST["qq"]), 0, 50) : "";//qq
			$aid = isset($_POST["aid"]) ? (int)$_POST["aid"] : "";//代理商id
			$agent_name = "";//代理商名称
			$sid = isset($_POST["sid"]) ? (int)$_POST["sid"] : "";//员工id
			$staff_name = "";//员工姓名
			$demo_account = isset($_POST["demo_account"]) ? (int)$_POST["demo_account"] : 0;//帐号类型,是否试玩帐号，0，否，1，是
			
			//交易规则
			$stock_type = isset($_POST["stock_type"]) ? (int)$_POST["stock_type"] : "";//融资融券类型，1：融资，2：融券，0：无限制
			$max_amount = isset($_POST["max_amount"]) ? round($_POST["max_amount"], 2) : 0;//最大持仓总额(资金)
			$buy_fee = isset($_POST["buy_fee"]) ? round($_POST["buy_fee"], 2) : 0;//买入手续费
			$sell_fee = isset($_POST["sell_fee"]) ? round($_POST["sell_fee"], 2) : 0;//卖出手续费
			$save_cost = isset($_POST["save_cost"]) ? round($_POST["save_cost"], 2) : 0;//结算费用
			$stamp = isset($_POST["stamp"]) ? round($_POST["stamp"], 2) : 0;//印花税
			$min_num = isset($_POST["min_num"]) ? (int)$_POST["min_num"] : 0;//最小股数
			$max_num = isset($_POST["max_num"]) ? (int)$_POST["max_num"] : 0;//最大股数
			$min_time = isset($_POST["min_time"]) ? (int)$_POST["min_time"] : 0;//最小卖出时间
			$rmin_time = isset($_POST["rmin_time"]) ? (int)$_POST["rmin_time"] : 0;//融券最小卖出时间
			$single_limit = isset($_POST["single_limit"]) ? (int)$_POST["single_limit"] : 0;//单笔额度限额
			$upper_limit = isset($_POST["upper_limit"]) ? (int)$_POST["upper_limit"] : 0;//单股持仓总额上限
			$money_max = isset($_POST["money_max"]) ? (int)$_POST["money_max"] : 0;//最高可用额度限制
			$multiple = isset($_POST["multiple"]) ? (int)$_POST["multiple"] : 0;//资金倍数
			$is_only_rstock = isset($_POST["is_only_rstock"]) ? (int)$_POST["is_only_rstock"] : 0;//是否只能做带R股票
			$is_early_jiancang = isset($_POST["is_early_jiancang"]) ? (int)$_POST["is_early_jiancang"] : 0;//是否提前禁止建仓
			
			//银行卡信息
			$realname = isset($_POST["realname"]) ? mb_substr(sql_format($_POST["realname"]), 0, 100) : "";//真实姓名
			$sex = isset($_POST["sex"]) ? mb_substr(sql_format($_POST["sex"]), 0, 2) : "";//性别
			$bank_account = isset($_POST["bank_account"]) ? mb_substr(sql_format($_POST["bank_account"]), 0, 20) : $realname;//银行开户账户名称
			$bank_name = isset($_POST["bank_name"]) ? mb_substr(sql_format($_POST["bank_name"]), 0, 100) : "";//收款银行
			$bank_code = isset($_POST["bank_code"]) ? mb_substr(sql_format($_POST["bank_code"]), 0, 100) : "";//银行账号
			$bank_address = isset($_POST["bank_address"]) ? mb_substr(sql_format($_POST["bank_address"]), 0, 100) : "";//开户行(支行名称)，如郑州交行东风路支行
			$mobile = isset($_POST["mobile"]) ? mb_substr(sql_format($_POST["mobile"]), 0, 11) : "";//手机号
			$atmpwd = isset($_POST["atmpwd"]) ? md5($_POST["atmpwd"]) : "";//取款密码

			//校验参数
			if(! $username){
				ajax_return(0, "用户名不能为空");
			}
			if(! $password){
				ajax_return(0, "密码不能为空");
			}
			if($aid < 0 || $sid < 0){
				ajax_return(0, "非法请求");
			}
			
			if(!$nickname){
				ajax_return(0, "昵称不能为空");
			}
			if(!$mobile){
				ajax_return(0, "手机号不能为空");
			} elseif(!chk_phone($mobile)){
				ajax_return(0, "手机号不正确");
			}
			if(!$realname){
				ajax_return(0, "姓名不能为空");
			}
			//查询代理
			$sql = "select username from agent where id = $aid";
			$agent = $this->db->query($sql)->row_array();
			if(! $agent){
				ajax_return(0, "代理商不存在");
			} else{
				$agent_name = $agent["username"];
			}
			
			//查询员工
			$sql = "select username from agent_staff where aid = $aid and id = $sid";
			$agent_staff = $this->db->query($sql)->row_array();
			if(! $agent_staff){
				ajax_return(0, "员工不存在");
			} else{
				$staff_name = $agent_staff["username"];
			}
			
			//查询帐号是否重复
			$sql = "select * from user where username = '$username'";
			$user = $this->db->query($sql)->row_array();
			if($user){
				ajax_return(0, "帐号已存在");
			}
			
			//查询姓名是否重复
			$sql = "select * from user where realname = '$realname'";
			$user = $this->db->query($sql)->row_array();
			if($user){
				ajax_return(0, "姓名已存在");
			}
			
			//查询手机是否重复
			$sql = "select * from user where mobile = '$mobile'";
			$user = $this->db->query($sql)->row_array();
			if($user){
				ajax_return(0, "手机号已存在");
			}
			
			//查询昵称是否重复
			$sql = "select * from user where nickname = '$nickname'";
			$user = $this->db->query($sql)->row_array();
			if($user){
				ajax_return(0, "昵称已存在");
			}
			$reg_date = time();
			
			//插入记录
			$sql = "insert into user (username,password,sex,nickname,realname,mobile,email,qq,reg_date,is_locked,is_disable,is_stop,stock_type,max_amount,buy_fee,sell_fee,stamp,min_num,max_num,min_time,rmin_time,single_limit,upper_limit,money_max,multiple,is_only_rstock,is_early_jiancang,aid,agent_name,sid,staff_name,demo_account,atmpwd,is_lower_cash,bank_name,bank_code,bank_account,bank_address) values ('$username','$password','$sex','$nickname','$realname','$mobile','$email','$qq','$reg_date','0','0','0','$stock_type','$max_amount','$buy_fee','$sell_fee','$stamp','$min_num','$max_num','$min_time','$rmin_time','$single_limit','$upper_limit','$money_max','$multiple','$is_only_rstock','$is_early_jiancang','$aid','$agent_name','$sid','$staff_name','$demo_account','$atmpwd','0','$bank_name','$bank_code','$bank_account','$bank_address')";
			$this->db->query($sql);
			$res =$this->db->insert_id();
			if(! $res){
				ajax_return(0, "添加失败");
			} else{
				//更新代理商员工数量
				$sql = "update agent set member_now = member_now + 1 where id = $aid";
				$this->db->query($sql);
				
				ajax_return(1, "添加成功", "/user/index");
			}
		}
		
		//查询所有代理商
		$sql = "select * from agent where is_locked = 0";
		$agent = $this->db->query($sql)->result_array();
		$this->assign("agent",$agent);
		$this->assign('config_sys', $this->config_sys);
		$this->display();
	}
	
	/**
	* 编辑
	* @param	int		$id //帐号
	* @param	string	$username //帐号
	* @param	string	$password //密码
	* @param	string	$sex //性别
	* @param	string	$nickname //昵称
	* @param	string	$email //邮箱
	* @param	string	$qq //qq
	* @param	int		$aid //代理商id
	* @param	string	$agent_name //代理商名称
	* @param	int		$sid //员工id
	* @param	string	$staff_name //员工姓名
	* @param	string	$demo_account //帐号类型,是否试玩帐号，0，否，1，是		
	//交易规则
	* @param	int		$stock_type //融资融券类型，1：融资，2：融券，0：无限制
	* @param	int		$max_amount //最大持仓总额(资金)
	* @param	float	$buy_fee //买入手续费
	* @param	float	$sell_fee //卖出手续费
	* @param	float	$save_cost //结算费用
	* @param	float	$stamp //印花税
	* @param	int		$min_num //最小股数
	* @param	int		$max_num //最大股数
	* @param	int		$min_time //最小卖出时间
	* @param	int		$rmin_time //融券最小卖出时间
	* @param	int		$single_limit //单笔额度限额
	* @param	int		$upper_limit //单股持仓总额上限
	* @param	int		$money_max //最高可用额度限制
	* @param	int		$multiple //资金倍数
	* @param	int		$is_only_rstock //是否只能做带R股票
	* @param	int		$is_early_jiancang //是否提前禁止建仓
	//银行卡信息
	* @param	string	$realname //真实姓名
	* @param	string	$sex //性别
	* @param	string	$bank_account //银行开户账户名称
	* @param	string	$bank_name //收款银行
	* @param	string	$bank_code //银行账号
	* @param	string	$bank_address//开户行(支行名称)，如郑州交行东风路支行
	* @param	string	$mobile //手机号
	* @param	string	$atmpwd //取款密码
	* @param	array	$result 	返回结果
	*/
	public function edit($id = 0){
		$id =intval($id);
		if(IS_POST){
			if(0 >= $id){
				ajax_return(0, "参数有误");
			}
			//基础信息
			$username = isset($_POST["username"]) ? substr(sql_format($_POST["username"]), 0, 20) : "";//帐号
			$password = empty($_POST["password"]) ? "" : md5($_POST["password"]) ;//密码
			$nickname = isset($_POST["nickname"]) ? mb_substr(sql_format($_POST["nickname"]), 0, 10) : "";//昵称
			$email = isset($_POST["email"]) ? mb_substr(sql_format($_POST["email"]), 0, 50) : "";//邮箱
			$qq = isset($_POST["qq"]) ? mb_substr(sql_format($_POST["qq"]), 0, 50) : "";//qq
			$aid = isset($_POST["aid"]) ? (int)$_POST["aid"] : "";//代理商id
			$agent_name = "";//代理商名称
			$sid = isset($_POST["sid"]) ? (int)$_POST["sid"] : "";//员工id
			$staff_name = "";//员工姓名
			$demo_account = isset($_POST["demo_account"]) ? (int)$_POST["demo_account"] : 0;//帐号类型,是否试玩帐号，0，否，1，是
			
			//交易规则
			$stock_type = isset($_POST["stock_type"]) ? (int)$_POST["stock_type"] : "";//融资融券类型，1：融资，2：融券，0：无限制
			$max_amount = isset($_POST["max_amount"]) ? round($_POST["max_amount"], 2) : 0;//最大持仓总额(资金)
			$buy_fee = isset($_POST["buy_fee"]) ? round($_POST["buy_fee"], 2) : 0;//买入手续费
			$sell_fee = isset($_POST["sell_fee"]) ? round($_POST["sell_fee"], 2) : 0;//卖出手续费
			$save_cost = isset($_POST["save_cost"]) ? round($_POST["save_cost"], 2) : 0;//结算费用
			$stamp = isset($_POST["stamp"]) ? round($_POST["stamp"], 2) : 0;//印花税
			$min_num = isset($_POST["min_num"]) ? (int)$_POST["min_num"] : 0;//最小股数
			$max_num = isset($_POST["max_num"]) ? (int)$_POST["max_num"] : 0;//最大股数
			$min_time = isset($_POST["min_time"]) ? (int)$_POST["min_time"] : 0;//最小卖出时间
			$rmin_time = isset($_POST["rmin_time"]) ? (int)$_POST["rmin_time"] : 0;//融券最小卖出时间
			$single_limit = isset($_POST["single_limit"]) ? (int)$_POST["single_limit"] : 0;//单笔额度限额
			$upper_limit = isset($_POST["upper_limit"]) ? (int)$_POST["upper_limit"] : 0;//单股持仓总额上限
			$money_max = isset($_POST["money_max"]) ? (int)$_POST["money_max"] : 0;//最高可用额度限制
			$multiple = isset($_POST["multiple"]) ? (int)$_POST["multiple"] : 0;//资金倍数
			$is_only_rstock = isset($_POST["is_only_rstock"]) ? (int)$_POST["is_only_rstock"] : 0;//是否只能做带R股票
			$is_early_jiancang = isset($_POST["is_early_jiancang"]) ? (int)$_POST["is_early_jiancang"] : 0;//是否提前禁止建仓
			
			//银行卡信息
			$realname = isset($_POST["realname"]) ? mb_substr(sql_format($_POST["realname"]), 0, 100) : "";//真实姓名
			$sex = isset($_POST["sex"]) ? mb_substr(sql_format($_POST["sex"]), 0, 2) : "";//性别
			$bank_account = isset($_POST["bank_account"]) ? mb_substr(sql_format($_POST["bank_account"]), 0, 20) : $realname;//银行开户账户名称
			$bank_name = isset($_POST["bank_name"]) ? mb_substr(sql_format($_POST["bank_name"]), 0, 100) : "";//收款银行
			$bank_code = isset($_POST["bank_code"]) ? mb_substr(sql_format($_POST["bank_code"]), 0, 100) : "";//银行账号
			$bank_address = isset($_POST["bank_address"]) ? mb_substr(sql_format($_POST["bank_address"]), 0, 100) : "";//开户行(支行名称)，如郑州交行东风路支行
			$mobile = isset($_POST["mobile"]) ? mb_substr(sql_format($_POST["mobile"]), 0, 11) : "";//手机号
			$atmpwd = isset($_POST["atmpwd"]) ? md5($_POST["atmpwd"]) : "";//取款密码

			//校验参数
			if(! $username){
				ajax_return(0, "用户名不能为空");
			}
			if($aid < 0 || $sid < 0){
				ajax_return(0, "非法请求");
			}
			
			if(!$nickname){
				ajax_return(0, "昵称不能为空");
			}
			if(!$mobile){
				ajax_return(0, "手机号不能为空");
			} elseif(!chk_phone($mobile)){
				ajax_return(0, "手机号不正确");
			}
			if(!$realname){
				ajax_return(0, "姓名不能为空");
			}
			//查询会员
			$sql = "select id, aid from user where id = $id";
			$row = $this->db->query($sql)->row_array();
			if(! $row){
				ajax_return(0, "会员不存在");
			}
			
			//查询代理
			$sql = "select username from agent where id = $aid";
			$agent = $this->db->query($sql)->row_array();
			if(! $agent){
				ajax_return(0, "代理商不存在");
			} else{
				$agent_name = $agent["username"];
			}
			
			//查询员工
			$sql = "select username from agent_staff where aid = $aid and id = $sid";
			$agent_staff = $this->db->query($sql)->row_array();
			if(! $agent_staff){
				ajax_return(0, "员工不存在");
			} else{
				$staff_name = $agent_staff["username"];
			}
			
			//查询帐号是否重复
			$sql = "select * from user where username = '$username' and id != $id";
			$user = $this->db->query($sql)->row_array();
			if($user){
				ajax_return(0, "帐号已存在");
			}
			//查询手机是否重复
			$sql = "select * from user where mobile = '$mobile' and id != $id";
			$user = $this->db->query($sql)->row_array();
			if($user){
				ajax_return(0, "手机号已存在");
			}
			
			//查询昵称是否重复
			$sql = "select * from user where nickname = '$nickname' and id != $id";
			$user = $this->db->query($sql)->row_array();
			if($user){
				ajax_return(0, "昵称已存在");
			}
			
			//查询姓名是否重复
			$sql = "select * from user where realname = '$realname' and id != $id";
			$user = $this->db->query($sql)->row_array();
			if($user){
				ajax_return(0, "姓名已存在");
			}
			$sql1 = "";
			if($atmpwd){
				$sql1 .= ", atmpwd = '$atmpwd'";
			}
			if($password){
				$sql1 .= ", password = '$password'";
			}
			$sql = "update user set username = '$username',sex = '$sex',nickname = '$nickname',realname = '$realname',mobile = '$mobile',email = '$email',qq = '$qq',stock_type = '$stock_type',max_amount = '$max_amount',buy_fee = '$buy_fee',sell_fee = '$sell_fee',stamp = '$stamp',min_num = '$min_num',max_num = '$max_num',min_time = '$min_time',rmin_time = '$rmin_time',single_limit = '$single_limit',upper_limit = '$upper_limit',money_max = '$money_max',multiple = '$multiple',is_only_rstock = '$is_only_rstock',is_early_jiancang = '$is_early_jiancang',aid = '$aid',agent_name = '$agent_name',sid = '$sid',staff_name = '$staff_name',demo_account = '$demo_account',bank_name = '$bank_name',bank_code = '$bank_code',bank_account = '$bank_account',bank_address = '$bank_address' " . $sql1 . " where id = $id";
			$this->db->query($sql);
			
			//更新代理商员工数量
			if ($aid != $row["aid"]) {
				//删除之前代理数量
				$sql = "update agent set member_now = member_now - 1 where id = " . $row["aid"];
				$this->db->query($sql);
				
				//增加现在代理数量
				$sql = "update agent set member_now = member_now + 1 where id = " . $aid;
				$this->db->query($sql);
			}
			
			//返回结果集
			ajax_return(1, "更新成功", "/user/index");
		} else{
			if(0 >= $id){
				$this->error("参数有误");
			}
			$sql = "select * from user where id = $id";
			$result = $this->db->query($sql)->row_array();
			if(! $result){
				$this->error("记录不存在");
			}
			
			//查询所有代理商
			$sql = "select * from agent";
			$agent = $this->db->query($sql)->result_array();
			$this->assign("agent",$agent);
		
			//查询所有员工
			$sql = "select * from agent_staff where aid = " . $result["aid"];
			$staff = $this->db->query($sql)->result_array();
			
			$this->assign("agent",$agent);
			$this->assign("staff",$staff);
			$this->assign("result", $result);
			
			$this->assign('config_sys', $this->config_sys);
			$this->display();
		}
	}
	
	/**
	* 更改状态
	* @param	int		$id			用户id
	* @param	string	$field		更新字段
	* @param	array	$result 	返回结果
	*/
	public function change($field = "", $id){
		if(IS_POST){
			//校验参数
			if(0 >= $id){
				ajax_return(0, "非法请求");
			}

			//查询记录
			$sql = "select is_$field from user where id = $id";
			$result = $this->db->query($sql)->row_array();
			if(! $result){
				ajax_return(0, "帐号不存在");
			}
			
			//判断当前状态
			if($result["is_" . $field] == 0){
				$is_field = 1;
			} else{
				$is_field = 0;
			}
			
			//更新记录
			$sql = "update user set is_$field = $is_field where id = $id";
			$this->db->query($sql);
			$return[$field] = $is_field;
			$return["id"] = $id;
			$return["field"] = $field;
			
			//返回结果集
			ajax_return(1, "状态已更新", $return);
		}
	}
	
    /**
	* 股票限制
	*/
    public function nforbid($uid = 0, $p = 1){
		//接收参数
        $uid = intval($uid);
        $p = intval($p);
        if (! $uid) $this->error("参数有误"); 
     
        //数据分页
		$size = 15;//每页数量
		if(0 >= $p){
			$p = 1;
		}
		$start = ($p - 1) * $size;
        
        //查询记录
        $sql = "select a.code, a.addtime, b.name from user_forbid a inner join stock_code b on a.code = b.code where a.uid = $uid";
        $list = $this->db->query($sql)->result_array();
        $total = count($list);
        
        //分页展示
        $list = array_slice($list, $start, $size);
		$url = "/" . $this->_controller . '/' . $this->_action . '/' . $uid . "/";
		$page = page_format($total, $size, $p, $url);
        
        //查询会员
        $sql = "select id, username from user where id = $uid";
        $row = $this->db->query($sql)->row_array();

        //分配变量
        $this->assign("list", $list);
        $this->assign("total", $total);
        $this->assign("page", $page);
        $this->assign("row", $row);
        $this->display();
	}
   
	/**
	* 根据代理商查询下属会员
	* @param	int		$aid		代理商ID
	* @param	array	$result 	返回结果
	*/
	public function ajax_get_user(){
		if (IS_POST) {
			$aid = isset($_POST["aid"]) ? (int)$_POST["aid"] : 0;
			
			//校验参数
			if (0 >= $aid) {
				ajax_return(0, "非法请求");
			}
			
			//查询记录
			$sql = "select id, username,nickname,realname,mobile,CASE WHEN `sex`='男' THEN '先生' ELSE '女士' END sex from user where aid = $aid";
			$result = $this->db->query($sql)->result_array();
			
			//返回结果
			ajax_return(1, "查询成功", $result);
		}
	}
	
	/**
	* 用户平账
	* @param	int		$uid		用户id
	* @param	array	$result 	返回结果
	*/
	public function ajax_set_user() {
		if (IS_POST) {
			$uid = isset($_POST["uid"]) ? (int)$_POST["uid"] : 0;
			if ($uid > 0) {
				//指定用户
				$where = " where id = $uid";
			} else {
				//全部用户
				$where = "";
			}
			
			//平账
			$sql = "select id, finance, amount, gain, username, demo_account from user" . $where;
			$user = $this->db->query($sql)->result_array();
			$num = 0;
			$log = "";
			foreach ($user as $key => $val) {
				$res = set_user($val["id"]);
				if (round($val["finance"], 2) != $res["finance"] || round($val["amount"], 2)  != $res["amount"] || round($val["gain"], 2) != $res["gain"]) {

					// dump(round($val["finance"], 2) != $res["finance"]);
					// dump(round($val["amount"], 2) != $res["amount"]);
					// dump(round($val["gain"], 2) != $res["gain"]);
					// dump(round($val["finance"], 2));
					// dump($res["gain"]);
					// die;
					
					//更新账户
					$sql = "update user set finance = {$res["finance"]}, amount = {$res["amount"]}, gain = {$res["gain"]}, set_time = CURRENT_TIMESTAMP where id = " . $val["id"];
					$this->db->query($sql);

					//日志文件
					$log .= "用户:{$val["id"]}:{$val["username"]}::保证金{$val["finance"]}~{$res["finance"]},可用额度:{$val["amount"]}~{$res["amount"]},盈亏:{$val["gain"]}~{$res["gain"]}".PHP_EOL;
					
					//统计数量
					$num ++;
				}
			}

			//记录日志
			if ($log) {
				file_put_contents("./data/logs/auto_set_user.log", date("Y-m-d H:i:s") . PHP_EOL . $log . PHP_EOL, FILE_APPEND);
			}
			
			//返回结果集
			ajax_return(1, "平账成功（" . $num . "）");
		}
	}
	
	/**
	* 获取用户详细信息
	* @param	int		$aid		代理商ID
	* @param	array	$result 	返回结果
	*/
	public function get_user_info(){
		if (IS_POST) {
			$id = isset($_POST["id"]) ? (int)$_POST["id"] : 0;
			
			//校验参数
			if (0 >= $id) {
				ajax_return(0, "非法请求");
			}
			
			//查询记录
			$result = get_user($id, 1);
			
			//格式化时间
			$result["reg_date"] = date("Y-m-d H:i:s", $result["reg_date"]);
			$result["last_login"] = date("Y-m-d H:i:s", $result["last_login"]);
			
			//返回结果
			ajax_return(1, "查询成功", $result);
		}
	}
    
    /**
	* 新增股票限制
	*/
	public function ajax_add_forbid(){
		if (IS_POST) {
            //接收参数
			$uid = isset($_POST["uid"]) ? (int)$_POST["uid"] : 0;
			$type = isset($_POST["type"]) ? (int)$_POST["type"] : 0;
			
            //设置参数
            if ($type != 1) $type = 0;
            if ($type == 0) {
                //股票代码
                $code = isset($_POST["code"]) ?  sql_format($_POST["code"]) : 0;
                if (! $code) ajax_return(0, "参数有误");
                
                //查询股票代码
                $sql = "select id from stock_code where code = '$code'";
                $row = $this->db->query($sql)->row_array();
                if (! $row) ajax_return(0, "股票代码有误");
                
                //查询记录
                $sql = "select uid from user_forbid where uid = $uid and code = '$code'";
                $row = $this->db->query($sql)->row_array();
                if ($row) ajax_return(0, "已添加限制列表，请勿重复添加");
                
                //新增记录
                $sql = "insert into user_forbid (uid, code) values ($uid, '$code')";
                $this->db->query($sql);
                
                //返回结果
                ajax_return(1, "成功添加限制列表");
            } else {
                //小盘股
                $money = isset($_POST["money"]) ? (int)$_POST["money"] : 0;
                if (! $money) ajax_return(0, "参数有误");
                
                //查询股票
                $sql = "select CONCAT(type, code) as code from `stock_code` where can_bull_down = 1 and can_bull_up = 1 and can_bull = 1";
                $stock = $this->db->query($sql)->result_array();
                $stock_code = array_column($stock, "code");
                $res = get_stock1($stock_code);
              
                //遍历数据
                foreach ($res as $key => $val) {
                    if ($money > $val[44]) {
                        $code = substr($key, 2);
                        $in_sql = "insert into user_forbid (uid, code) values ($uid, '$code')";
                        $this->db->query($in_sql);
                    }
                }

                //返回结果集
                ajax_return(1, "成功添加限制列表");
            }
		}
	}
    
    /**
	* 删除股票限制
	*/
	public function ajax_delete_forbid(){
		if (IS_POST) {
            //接收参数
			$code = isset($_POST["code"]) ? $_POST["code"] : 0;
			$uid = isset($_POST["uid"]) ? (int)$_POST["uid"] : 0;
			
            //设置参数
            if ($code == "all") {
                $sql = "delete from user_forbid where uid = $uid";
            } else {
                $sql = "delete from user_forbid where uid = $uid and code = '$code'";
            }
            $this->db->query($sql);
			
			//返回结果
			ajax_return(1, "删除成功");
		}
	}
}
